package com.aj.activity.fragment;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;

import com.aj.activity.R;
import com.aj.activity.SharePhotoActivity;
import com.aj.activity.adapter.FamilyPhotoGridAdapter;
import com.aj.databases.entry.FamilyZoneInfo;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.GetAlbumPhotoRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.http.response.GetAlbumPhotoResponse;
import com.aj.utils.Contants;

public class ChooseHomePhotoFragment extends FamilyBaseFragment {

	private GridView photo_body;
	private static final String TAG = ChooseHomePhotoFragment.class.getName();

	private List<FamilyZoneInfo> familyZoneInfoList;
	private FamilyPhotoGridAdapter familyPhotoGridAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = LayoutInflater.from(getActivity()).inflate(
				R.layout.fragment_choose_home_photo, null);
		initView(view);

		return view;
	}

	private void initView(View view) {
		photo_body = (GridView) view.findViewById(R.id.photo_body);
		new GetAlbumPhotoRequest().excute(getActivity(), getActivity()
				.getIntent().getStringExtra(Contants.USER_ID_PARAMS), "4", 0,
				50, new IHttpCallBack() {

					@Override
					public void success(String result) {
						try {
							synchronized (SharePhotoActivity.isLoadFlag) {
								if (SharePhotoActivity.isLoadFlag) {
									Log.e(TAG, "ChooseHomePhotoFragment隐藏的");
									SharePhotoActivity.instance.dimiss();
								} else {
									SharePhotoActivity.isLoadFlag = true;
								}
							}

							dimissProgressDialog();
							GetAlbumPhotoResponse respone = new GetAlbumPhotoResponse(
									new JSONObject(result));
							Common common = respone.getCommon();
							if (common == null) {

							} else if (common.getReturnCode().equals(
									BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
								familyZoneInfoList = respone
										.getAlbumPhotoInfoList();
								familyPhotoGridAdapter = new FamilyPhotoGridAdapter(
										getActivity(), familyZoneInfoList, "");
								photo_body.setAdapter(familyPhotoGridAdapter);
							} else {
								Toast.makeText(getActivity(),
										common.getErrorMsg(), Toast.LENGTH_LONG)
										.show();
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void fail(String result) {
						synchronized (SharePhotoActivity.isLoadFlag) {
							if (SharePhotoActivity.isLoadFlag) {
								SharePhotoActivity.instance.dimiss();
							} else {
								SharePhotoActivity.isLoadFlag = true;
							}
						}
					}
				});
	}
}
